package com.guodi.model.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.core.tool.api.R;
import com.guodi.model.entity.ApiIdtFillTaskOpinion;
import com.guodi.model.entity.IdtFillTaskOpinion;
import com.guodi.model.enums.IdtFillTaskStatusEnum;
import com.guodi.model.mapper.IdtFillTaskOpinionMapper;
import com.guodi.model.service.IIdtFillTaskOpinionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.model.service.IIdtFillTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 指标填报任务意见表 服务实现类
* @author lzy
* @date 2023-05-23
*/
@Service
public class IdtFillTaskOpinionServiceImpl extends ServiceImpl<IdtFillTaskOpinionMapper, IdtFillTaskOpinion> implements IIdtFillTaskOpinionService {

    @Resource
    private IdtFillTaskOpinionMapper idtFillTaskOpinionMapper;
    @Resource @Lazy
    private IIdtFillTaskService idtFillTaskService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveOpinions(List<ApiIdtFillTaskOpinion> opinions) {
        if(CollectionUtils.isEmpty(opinions)){
            return R.data(null);
        }
        String taskId = opinions.get(0).getTaskId();
        List<IdtFillTaskOpinion> saveList = new ArrayList<>();
        List<IdtFillTaskOpinion> updateList = new ArrayList<>();

        for(ApiIdtFillTaskOpinion apiOpinion : opinions){
            IdtFillTaskOpinion opinion = new IdtFillTaskOpinion();
            BeanUtils.copyProperties(apiOpinion,opinion);
            if(StringUtils.isBlank(apiOpinion.getId())){
                prepareData(opinion,true);
                saveList.add(opinion);
            }else {
                prepareData(opinion,false);
                updateList.add(opinion);
            }
        }

        if(CollectionUtils.isNotEmpty(saveList)){
            this.saveBatch(saveList);
        }
        if(CollectionUtils.isNotEmpty(updateList)){
            this.updateBatchById(updateList);
        }
        //需要对任务做状态处理
        idtFillTaskService.editStatus(taskId, IdtFillTaskStatusEnum.COMPLETED.getCode());
        return R.data("保存成功");
    }

    private void prepareData(IdtFillTaskOpinion entity, boolean isSave) {
        if(isSave){
            entity.setId(UuidUtil.get32UUID());
            entity.setCreator(String.valueOf(AuthUtil.getUserId()));
            entity.setCreateTime(new Date());
            //防止误修改
            entity.setModifier(null);
            entity.setModifyTime(null);
        }else{
            entity.setModifier(String.valueOf(AuthUtil.getUserId()));
            entity.setModifyTime(new Date());
            //防止误修改
            entity.setCreator(null);
            entity.setCreateTime(null);
        }
    }
}
